<?php

namespace App\Http\Middleware;

use App\Http\Services\MsgService;
use App\Http\Services\RuleService;
use Closure;

class VerifyRbac
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $admin = $request->input('admin');

        $rules = RuleService::getMenu($admin['id'], false);
        $rules = array_column($rules['rules'], 'url');
        $path = $request->path(); //当前访问的路由
        if(in_array('/'.$path, $rules) || in_array($path,$this->whitelist)){
            $request->merge([
                'rbac' => $rules,

            ]);
            return $next($request);
        }
        return MsgService::getMsg(10007);
    }

    //无需拦截白名单
    protected $whitelist = [
        "admin/index/index",
        "admin/index/getMenu",
        "admin/index/todo",
        "admin/upload",
        "admin/question/edit",
        "admin/admin/getAdmin",
        "admin/question/handle",

    ];
}
